package com.kevin.mybatis.dao;

import com.kevin.mybatis.object.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * Created by loli on 2015/3/28.
 */
public class UserDao {
    private static SqlSessionFactory sqlSessionFactory;

    static {

        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader("Configuration.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public User selectUserById(int id){
        SqlSession session = null;
        User user = null;
        try {
            session = sqlSessionFactory.openSession();
            user = session.selectOne("com.kevin.mybatis.dao.IUserOperation.selectUserById", id);
        }finally {
            session.close();
        }
        return user;
    }

    public static void main(String[] args){
        UserDao userDao = new UserDao();
        User user = userDao.selectUserById(1);
        System.out.println(user.getName());
        System.out.println(user.getPassword());

        SqlSession session = sqlSessionFactory.openSession();
        IUserOperation userOperation = session.getMapper(IUserOperation.class);
        userOperation.selectUserById(1);


    }
}
